/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.tools;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.jfinal.render.Render;
import erwebma.client.j2se.MatrixToImageWriter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
import javax.imageio.ImageIO;

/**
 *
 * @author Administrator
 */
public class QrCodeRender extends Render {

    private File file;
    private String data;

    public QrCodeRender(String data) {

        this.data = data;
    }

    @Override
    public void render() {
        try {
         
            if (data != null) {
                int width = 128;
                int height = 128;
                String format = "png";
                //data  = AddCode.encode(data);
                data = "{\"tel\":{\"tel\":\"" + data + "\"}}";
                Hashtable hints = new Hashtable();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, height, hints);
//    File outputFile = new File(path + new Date().getTime() + ".png");
                //MatrixToImageWriter.toBufferedImage(bitMatrix);
                BufferedImage bufi = MatrixToImageWriter.toBufferedImage(bitMatrix);
                response.reset();
                response.setContentType("image/png");
                ImageIO.write(bufi, "png", response.getOutputStream());
            }
        } catch (Exception e) {
        }
    }

}
